/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * panelHorario.java
 *
 * Created on Apr 16, 2011, 12:59:31 PM
 */

package frontera.interfaz.panel;

import entidad.Horario;
import entidad.HorarioDia;
import java.awt.Color;
import java.awt.Dimension;
import java.util.List;
import javax.swing.JScrollPane;
import javax.swing.JTable;

/**
 *
 * @author Nico
 */
public class PanelHorario extends javax.swing.JPanel {

    /** Creates new form panelHorario */
    public PanelHorario(Horario horario)
    {
      initComponents();
      this.horario = horario.getHorario();

      inicializarHoras(scrollHoras, tablaHoras);
      inicializarDia(scrollLunes,tablaLunes,1);
      inicializarDia(scrollMartes,tablaMartes,2);
      inicializarDia(scrollMiercoles,tablaMiercoles,3);
      inicializarDia(scrollJueves,tablaJueves,4);
      inicializarDia(scrollViernes,tablaViernes,5);
      inicializarDia(scrollSabado,tablaSabado,6);
      inicializarDia(scrollDomingo,tablaDomingo,7);
    }
    
    public final void inicializarDia(JScrollPane scrollPanel,JTable tabla,int i ){
      tabla =  new javax.swing.JTable(24,1);
      tabla.setBackground(new Color(154,205,50));
      
      tabla.setCellSelectionEnabled(true);
      tabla.setRowHeight(14);
      tabla.setSize(64, 477);
      tabla.setMaximumSize(new Dimension(60,400));
      tabla.getColumnModel().getColumn(0).setHeaderValue(columnas[i]);
      scrollPanel = new javax.swing.JScrollPane(tabla);
      panelHorario.add(scrollPanel);
      
      int indice = buscarIndice(horario, i-1);
      if((horario.get(indice).getFinJor()!=horario.get(indice).getInicioJor() )){
          
          int inicioJornada = horario.get(indice).getInicioJor();
          int finJornada = horario.get(indice).getFinJor() - 1;
         
          tabla.setRowSelectionInterval(inicioJornada, finJornada);
          tabla.setColumnSelectionInterval(0, 0);
      }
    }
    
    private int buscarIndice(List<HorarioDia> horarioDia, int ID){
        for (HorarioDia hd: horarioDia){
            if (hd.getIdDia() == ID){
                return horarioDia.indexOf(hd);
            }
        }
        
        return -1;
    }

    public final void inicializarHoras(JScrollPane scrollPanel,JTable tabla){
        tabla = new javax.swing.JTable(24,1);
        tabla.setEnabled(false);
        tabla.setBackground(new Color(0,0,255));
        tabla.setRowHeight(14);
        tabla.setSize(64, 477);
        tabla.setMaximumSize(new Dimension(60,400));
        tabla.getColumnModel().getColumn(0).setHeaderValue(columnas[0]);
        for (int i = 1;i<25;i++)
        {
            tabla.setValueAt(String.valueOf(i)+":00",i-1 ,0);
        }
        scrollPanel = new javax.swing.JScrollPane(tabla);
        panelHorario.add(scrollPanel);
     }
    public void seleccionarHoras(int idDia, int iniJor,int finJor){
        JScrollPane panel = (JScrollPane) panelHorario.getComponent(idDia);//getComponents().setVisible(false);
        
        JTable tabla = (JTable)panel.getViewport().getComponent(0);
        if (iniJor<finJor)
        {
        tabla.setRowSelectionInterval(iniJor, finJor);
        tabla.setColumnSelectionInterval(0, 0);
        }

    }
    public Horario getHorarioSeleccionado(){
        Horario horarioSeleccionado = new Horario();
        for(HorarioDia hd: horarioSeleccionado.getHorario())
        {
            JScrollPane panel = (JScrollPane)panelHorario.getComponent(hd.getIdDia()+1);
            JTable tabla = (JTable)panel.getViewport().getComponent(0);
            int anchor = tabla.getSelectionModel().getAnchorSelectionIndex();
            int lead = tabla.getSelectionModel().getLeadSelectionIndex();
            
            if(anchor != -1 && lead != -1)
            {
                if(anchor == lead)
                {
                    lead++;
                    hd.setFinJor(lead);
                }
                
                else if (lead > anchor) {
                    hd.setFinJor(lead+1);
                }
                hd.setInicioJor(anchor);
                hd.setIdDia(hd.getIdDia());
            }
        }
        return horarioSeleccionado;
       }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelHorario = new javax.swing.JPanel();

        panelHorario.setLayout(new java.awt.GridLayout(1, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelHorario, javax.swing.GroupLayout.DEFAULT_SIZE, 676, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelHorario, javax.swing.GroupLayout.DEFAULT_SIZE, 476, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel panelHorario;
    // End of variables declaration//GEN-END:variables
    private javax.swing.JTable tablaHoras;
    private javax.swing.JTable tablaLunes;
    private javax.swing.JTable tablaMartes;
    private javax.swing.JTable tablaMiercoles;
    private javax.swing.JTable tablaJueves;
    private javax.swing.JTable tablaViernes;
    private javax.swing.JTable tablaSabado;
    private javax.swing.JTable tablaDomingo;
    private javax.swing.JScrollPane scrollHoras;
    private javax.swing.JScrollPane scrollLunes;
    private javax.swing.JScrollPane scrollMartes;
    private javax.swing.JScrollPane scrollMiercoles;
    private javax.swing.JScrollPane scrollJueves;
    private javax.swing.JScrollPane scrollViernes;
    private javax.swing.JScrollPane scrollSabado;
    private javax.swing.JScrollPane scrollDomingo;
    private List<HorarioDia> horario;
    private String[] columnas= {"Horas","Lunes","Martes","Miercoles","Jueves",
    "Viernes","Sabado","Domingo"};

}
